package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 运镖任务
 */
@Slf4j
public class DartCarTaskImpl extends AbstractTask {

    public DartCarTaskImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public String getTaskName() throws Exception {
        return "运镖";
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc1();
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        // 找到“日常活动”并打开
        if (!findAndClickActivity()) {
            return false;
        }

        // 找到运镖并参加
        Boolean canYuRes = findAndClickTaskTitle(PicConstants.运镖任务_任务标题,
                new String[]{PicConstants.运镖任务_完成图标,PicConstants.运镖任务_完成图标2});
        if (canYuRes == null || !canYuRes) {
            return canYuRes;
        }

        ThreadUtil.sleep2();
        return true;
    }

    @Override
    public Boolean after() throws Exception {
        int confirmCount = 0;
        // 限制最大循环次数
        int times = 0;
        while (times < 40) {
            times++;
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_长安城小图标,1).isFinded()) {
                ThreadUtil.sleep1();
                if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.运镖任务_接受, 1)) {
                    return true;
                } else {
                    FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.运镖任务_确认, 1);
                    confirmCount++;
                    TaskProgressHandler.updateProgress(getTaskName(), confirmCount + "次");
                }
            }
            ThreadUtil.sleep(20000);
        }
        return false;
    }
}
